package com.sinux.mms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinux.mms.dao.SuserDaoI;
import com.sinux.mms.entity.Suser;
import com.sinux.mms.service.SuserServiceI;
import com.sinux.mms.util.DateTools;

/**
 * @author WangGan
 * @version 2017年6月14日 上午11:48:16 
 * 
 */
@Service
public class SuserServiceImpl implements SuserServiceI {
	@Autowired
	private SuserDaoI userDao;

	@Override
	public List<Suser> findAllUserList() {
		String hql="from Suser";
		return userDao.find(hql);
	}

	@Override
	public Suser login(Suser user) {
		String hql="from Suser u where (u.logname =:logname or u.phone=:logname) and u.pwd = :pwd ";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("logname", user.getLogname());
		params.put("pwd", user.getPwd());
		
		List<Suser> list=userDao.find(hql, params);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public void reg(Suser user) throws Exception {
		// TODO Auto-generated method stub
		user.setCdate(DateTools.getCurrentTime());
		//user.setId(System.currentTimeMillis()+"");//时间戳为id
		userDao.save(user);
	}

	@Override
	public void add(Suser user) throws Exception {
		// TODO Auto-generated method stub
		user.setCdate(DateTools.getCurrentTime());
		userDao.save(user);
	}

	@Override
	public Suser get(String id) {
		// TODO Auto-generated method stub
		Suser suser=null;
		if(StringUtils.isNotEmpty(id)) {
			suser=userDao.get(Suser.class, id);
		}
		return suser;
	}

	@Override
	public void edit(Suser user) throws Exception {
		Suser suser=userDao.get(Suser.class, user.getId());
		if(suser!=null) {
			BeanUtils.copyProperties(user, suser, new String[] {"id","logname","pwd"});
			suser.setCdate(DateTools.getCurrentTime());
			userDao.update(suser);
		}
	}

	@Override
	public void delete(String id) {
		Suser suser=userDao.get(Suser.class, id);
		if(suser!=null) {
			userDao.delete(suser);
		}
	}

	@Override
	public void grant(String ids, Suser user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void editPwd(Suser user) {
		Suser suser=userDao.get(Suser.class, user.getId());
		if(suser!=null&&StringUtils.isNotEmpty(user.getPwd())) {
			suser.setPwd(user.getPwd());
			userDao.update(suser);
		}
		
	}

	@Override
	public List<Suser> loginCombobox(String q) {
		// TODO Auto-generated method stub
		return null;
	}

}
